<template>
  <div id="chartBar" :style="{ width: '100%', height: '100%' }"></div>
</template>

<script>
export default {
  props: {
    barData: {
      type: Object,
      default: () => {}
    },

    title: {
      type: String,
      default: '数据表'
    }
  },

  computed: {
    chartOption() {
      return {
        title: {
          text: `${this.title}（前10排名，单位：百万）`,
          left: 'center'
        },
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'shadow'
          }
        },
        xAxis: {
          type: 'value',
          boundaryGap: [0, 0.01]
        },
        yAxis: {
          type: 'category',
          data: this.barData.dataA
        },
        series: [
          {
            name: '本季度违规用户量（前10排名）',
            type: 'bar',
            data: this.barData.dataB,
            barWidth: 20,
            itemStyle: {
              normal: {
                color: '#00CCFF',
                label: { show: true },
                lineStyle: {
                  color: '#00CCFF'
                }
              }
            }
          }
        ]
      }
    }
  },

  watch: {
    barData() {
      this.drawLine()
    }
  },

  mounted() {
    this.drawLine()
  },

  methods: {
    drawLine() {
      // 基于准备好的dom，初始化echarts实例
      let myChart = this.$echarts.init(document.getElementById('chartBar'))
      // 绘制图表
      myChart.setOption(this.chartOption)
    }
  }
}
</script>

<style lang="scss" scoped></style>
